package va.radiomaria.gui;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
 * Provides a like button that allows the user to indicate his/her preference of a song.
 * Button sends a like command every time the button changed from deactivated to activated.
 */
public class LikePanel extends JPanel implements MouseListener {

	private static final long serialVersionUID = 1L;
	
	private ActionListener controller = null;
		
	private boolean liked = false; 
	
	private String likedIconPath = "Icons/like1.png"; //image displayed when "like" is active
	private String unlikedIconPath = "Icons/like2.png"; //image displayed when "like" is not active
	
	private BufferedImage likedImg = null;
	private BufferedImage unlikedImg = null;

	
	public LikePanel(ActionListener controller) {
		this.controller = controller;
		this.setBackground(MainView.BACKGROUND_COLOR);
		addMouseListener(this);
		try {
			likedImg = ImageIO.read(new File(likedIconPath));
			unlikedImg = ImageIO.read(new File(unlikedIconPath));

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void toggleLiked() {
		liked = !liked;
		repaint();
		if(liked) {
			controller.actionPerformed(new ActionEvent(this, -1, CommonActionCommand.I_LIKE.toString()));		
		}
	}
	
	public void resetLiked() {
		liked = false;
	}
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		g.drawImage(getCurrentImage(), 0, 0, null);
	}	
	
	

	private Image getCurrentImage() {
		if(liked) {
			return likedImg;
		}
		else {
			return unlikedImg;
		}
	}


	@Override
	public void mouseClicked(MouseEvent mouseEvent) {
		toggleLiked();
	}

	/*
	 * Unused MouseListener Events
	 */
	
	@Override
	public void mouseEntered(MouseEvent arg0) {
	}

	@Override
	public void mouseExited(MouseEvent arg0) {		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
	}
}